package com.atwork.wuhua.ui.activity;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ZoomControls;

import com.atwork.wuhua.ConstantsMyself;
import com.atwork.wuhua.R;
import com.atwork.wuhua.base.BaseActivity;
import com.atwork.wuhua.utils.LogUtils;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.UiSettings;
import com.baidu.mapapi.model.LatLng;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MapActivity extends BaseActivity {

    @BindView(R.id.mmap)
    MapView mapView;

    //初始化地图控制器对象
    private BaiduMap mBaiduMap;
    private MyLocationListenner myListener = new MyLocationListenner();
    private LocationClient mLocClient;

    private LatLng mLatlng;
    //自定义定位小蓝点的Marker
    private Marker locationMarker;

    private String lat, log;

    @Override
    protected boolean isBindEventBus() {
        return false;
    }

    @Override
    public int intiLayout() {
        return R.layout.activity_map;
    }

    @Override
    public void initView() {
        if (getIntent().getExtras() != null) {
            lat = getIntent().getExtras().getString(ConstantsMyself.INTENTLAT);
            log = getIntent().getExtras().getString(ConstantsMyself.INTENTLOG);
        }


        initMap();
    }

    private void initMap() {
        mBaiduMap = mapView.getMap();
        mBaiduMap.setMyLocationEnabled(true);// // 开启定位图层
        mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL); //设置为普通地图

        // 定位初始化
        mLocClient = new LocationClient(this);
        mLocClient.registerLocationListener(myListener);
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);// 设置定位模式 高精度
        option.setOpenGps(true); // 打开gps
        option.setCoorType("bd09ll"); // 设置坐标类型
        option.setScanSpan(1000);
        option.setIsNeedAddress(false);// 设置定位结果包含地址信息
        option.setNeedDeviceDirect(true);// 设置定位结果包含手机机头 的方向
        mLocClient.setLocOption(option);
        mLocClient.start();

        UiSettings uiSettings = mBaiduMap.getUiSettings();
        mapView.showZoomControls(false);//缩放按钮
        mapView.showScaleControl(false);//控制比例尺控件是否显示
        uiSettings.setCompassEnabled(false);//指南针
        uiSettings.setRotateGesturesEnabled(false);//旋转手势
        uiSettings.setOverlookingGesturesEnabled(false);//地图俯视
        // 隐藏logo
        View child = mapView.getChildAt(1);
        if (child != null && (child instanceof ImageView || child instanceof ZoomControls)) {
            child.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public void initData() {

    }

    private void movePoionMap() {
        //定义坐标点
        LatLng point = new LatLng(Float.parseFloat(lat), Float.parseFloat(log));

        View view = View.inflate(this, R.layout.view_scenic_maker, null);
        // 填充数据
        TextView nameView = (TextView) view.findViewById(R.id.maker_name);
        nameView.setText("百度地图SDK");
        // 构建BitmapDescriptor
        BitmapDescriptor bitmap = BitmapDescriptorFactory.fromView(view);

        //构建MarkerOption，用于在地图上添加Marker
        OverlayOptions option = new MarkerOptions()
                .position(point)
                .title("百度地图SDK")
                .icon(bitmap);

        //在地图上添加Marker，并显示
        mBaiduMap.addOverlay(option);


        //移动位置
        MapStatus.Builder builder = new MapStatus.Builder();
        builder.target(point);
        builder.zoom(19f);
        mBaiduMap.setMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
    }


    @OnClick(R.id.img_left)
    public void onViewClicked() {
        finish();
    }

    /**
     * 定位SDK监听函数
     */
    public class MyLocationListenner implements BDLocationListener {

        @Override
        public void onReceiveLocation(BDLocation location) {
            // map view 销毁后不在处理新接收的位置
            if (location == null || mapView == null) {
                return;
            }

            mLatlng = new LatLng(location.getLatitude(), location.getLongitude());
            LogUtils.e("lat==>" + location.getLatitude());
            LogUtils.e("log==>" + location.getLongitude());
            //展示自定义定位小蓝点
            if (locationMarker == null) {
                //首次定位
//                locationMarker = (Marker) mBaiduMap.addOverlay(new MarkerOptions().position(mLatlng)
//                        .icon(BitmapDescriptorFactory.fromResource(R.mipmap.gps_point))
//                        .anchor(0.5f, 0.5f));
//                //首次定位,选择移动到地图中心点并修改级别到15级
//                MapStatus.Builder builder = new MapStatus.Builder();
//                builder.target(mLatlng);
//                builder.zoom(17f);
//                mBaiduMap.setMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));

                movePoionMap();
            }
        }
    }


    @Override
    public void onResume() {
        mapView.onResume();
        super.onResume();
    }

    @Override
    public void onPause() {
        mapView.onPause();
        super.onPause();
    }

    @Override
    protected void onDestroy() {
        mLocClient.stop();
        mBaiduMap.setMyLocationEnabled(false);
        mapView.onDestroy();
        mapView = null;
        super.onDestroy();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //判断用户是否点击了“返回键”
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            //与上次点击返回键时刻作差
            finish();
            return true;
        }


        return super.onKeyDown(keyCode, event);
    }
}
